using System;
using System.Runtime.Serialization;
using Bbc.Json;
using Scrobbler;

namespace Bbc.Scrobblers
{
    public abstract class BbcJsonStreamSource : BbcStreamSource, IStreamSource
    {
        internal override ISong DeserializeAndGetCurrentSong(string doc)
        {
            Playlist songs = Scrobbler.Utils.Deserializer<Playlist>.DeserializeJson(doc);
            if (songs.nowplaying == null || songs.nowplaying.Count == 0) { throw new SerializationException("Cannot deserialize JSON document."); }

            PlaylistItem latest = songs.nowplaying[0];
            return new Song
                       {
                           Artist = latest.artist,
                           Title = latest.title,
                           Duration = GetDuration(),
                           TimeStarted = DateTime.Now,
                       };
        }
    }
}